package com.biqee.service.tb.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.biqee.exception.Assert;
import com.biqee.model.TaobaoParseReulst;
import com.biqee.model.db.TbItem;
import com.biqee.service.tb.TaobaoParseService;
import com.biqee.service.tb.TbItemService;
import com.biqee.service.tb.TbQueryListParser;
import com.biqee.util.HtmlParseUtil;
import com.biqee.util.JsoupUtil;
import com.biqee.util.NumberUtil;
import com.biqee.util.StringUtil;
import com.biqee.util.UrlUtil;

public class TbQueryListParserImpl implements TbQueryListParser {
	private static final Logger logger = Logger
			.getLogger(TbQueryListParserImpl.class);

    private TaobaoParseService taobaoParseService ;
    
    private TbItemService tbItemService;

	@Override
	public List<TaobaoParseReulst> parseHtml(String html) {
		Assert.assertNotNull(html);
		List<TaobaoParseReulst> result = new ArrayList<TaobaoParseReulst>();
		Document doc = JsoupUtil.createDocument(html);
		if (doc != null) {
		
			Elements items = doc.select("li.list-item");
			if (items != null)
				for (int i = 0; i < items.size(); i++) {
					try {
						TaobaoParseReulst  itemTb = parseItem(items.get(i));
						
						if (itemTb != null) {
							result.add(itemTb);
							Thread.sleep(1000);
							logger.info("sleep 1000 ms, for next");
						}
					} catch (Exception e) {
						logger.error("parse item error, text="
								+ items.get(i).text(), e);
					}
				}
		}
		return result ;

	}
	
	private TaobaoParseReulst parseItem(Element item) {
		Element detailItem = item.select("div.photo a").first();
		if (detailItem == null)
			return null;
		String detailUrl = detailItem.attr("href");
		Element photoItem = item.select("div.photo img").first();
		String listLogo = photoItem.attr("data-lazyload-src");
		if(StringUtil.isBlank(listLogo)){
			listLogo = photoItem.attr("data-ks-lazyload");
		}
		if(StringUtil.isBlank(listLogo)){
			listLogo = photoItem.attr("src");
		}
		String tbItemId = UrlUtil.getParam(detailUrl, "id");
		TaobaoParseReulst result = taobaoParseService.parse(tbItemId);
		if(result!=null&&result.getTbItem()!=null){
			TbItem tbItem = result.getTbItem();
			tbItem.setListLogo(listLogo);
			tbItem.setDetailUrl(detailUrl);
			String postFee = item.select("li.price span").first().text();
			postFee = HtmlParseUtil.getNumber(postFee);
			tbItem.setPostFee(NumberUtil.toDouble(postFee));
			tbItemService.save(tbItem);
			
		}
		if(StringUtil.isBlank(listLogo)){
			logger.warn("listLogo is null,tbItemId="+tbItemId+",\n\r"+photoItem.toString());
		}
		return result ;
	}
	
//
//	/**
//	 * 
//	 * <li class="list-item last-item">
//	 * <h3 class="summary"><a title="【3周年2-6折】包邮AMPM女高领摇粒绒长袖T恤613503"
//	 * class="EventCanSelect" target="_blank" href="http://ju.atpanel.com/?url=http://item.taobao.com/item.htm?id=7254817376&amp;ad_id=&amp;am_id=&amp;cm_id=&amp;pm_id=1500206164949e8479ce"
//	 * stat="lf_aclog=2-6dda055eb56b5b5c5ffcb6dc204564b4-40-all-1&amp;lf_acfrom=0&amp;at_alitrackid=www.taobao.com"
//	 * >【3周年2-6折】包邮<span class="H">AMPM</span>女高领摇粒绒长袖<span
//	 * class="H">T恤</span>613503</a></h3>
//	 * <div class="photo"> <a target="_blank" href="http://ju.atpanel.com/?url=http://item.taobao.com/item.htm?id=7254817376&amp;ad_id=&amp;am_id=&amp;cm_id=&amp;pm_id=1500206164949e8479ce"
//	 * stat="lf_aclog=2-6dda055eb56b5b5c5ffcb6dc204564b4-40-all-1&amp;lf_acfrom=0&amp;at_alitrackid=www.taobao.com"
//	 * ><span><img class="hesper:small2big" src="http://img02.taobaocdn.com/bao/uploaded/i2/T10Rd8XdJjXXXN9EsT_013316.jpg_sum.jpg"
//	 * alt=""></span></a> </div>
//	 * <ul class="attribute">
//	 * <li class="legend2">
//	 * <a href="http://www.taobao.com/go/act/315/xfzbz_rsms.php"
//	 * class="xb-as-fact" title="卖家承诺消费者保障服务"
//	 * target="_blank"><span>消费者保障</span></a><a
//	 * href="http://www.taobao.com/go/act/315/xbqt090304.php"
//	 * class="xb-sevenday-return" title="卖家承诺7天无理由退换货"
//	 * target="_blank"><span>七天退换</span></a> <a
//	 * href="http://www.taobao.com/go/act/315/xfzbz_zpbz.php"
//	 * class="xb-quality_item" title="卖家承诺正品保障"
//	 * target="_blank"><span>正品保障</span></a></li>
//	 * <li class="sale">最近成交136笔</li>
//	 * <li class="place">上海<a title="淘宝代购" class="tao1site" target="_blank"
//	 * href="http://qudao.taobao.com/channelt/s9364759a707947ed8d0282cacaa84ea5.e?productID=7254817376"
//	 * >淘宝代购</a></li>
//	 * <li class="price"><em>59.00</em> <span class="shipping">运费：0.00</span></li>
//	 * </ul>
//	 * <div class="icon-area"><a href="http://www.tmall.com/" target="_blank"
//	 * class="mall-icon"></a></div>
//	 * <p class="seller lister hCard">
//	 * <a target="_blank" href=
//	 * "http://store.taobao.com/shop/view_shop.htm?user_number_id=134363478"
//	 * >美特斯邦威官方网店</a> <span data-icon="large" data-item="7254817376"
//	 * data-display="inline" data-nick="美特斯邦威官方网店" class="ww-light ww-large"><a
//	 * target="_blank" href="http://www.taobao.com/webww/?ver=1&amp;&amp;touid=cntaobao%E7%BE%8E%E7%89%B9%E6%96%AF%E9%82%A6%E5%A8%81%E5%AE%98%E6%96%B9%E7%BD%91%E5%BA%97&amp;siteid=cntaobao&amp;status=2&amp;portalId=&amp;gid=7254817376&amp;itemsId="
//	 * class="ww-inline ww-online"
//	 * title="点此可以直接和卖家交流选好的宝贝，或相互交流网购体验，还支持语音视频噢。"><span>旺旺在线</span></a></span>
//	 * </p>
//	 * </li>
//	 * 
//	 * @param item
//	 * @return
//	 */
//	private TbItem parseItem(Element item) {
//		Element detailItem = item.select("div.photo a").first();
//		if (detailItem == null)
//			return null;
//		String detailUrl = detailItem.attr("href");
//		Element photoItem = item.select("div.photo img").first();
//		String listLogo = photoItem.attr("data-lazyload-src");
//		String tbItemId = UrlUtil.getParam(detailUrl, "id");
//		String title = item.getElementsByAttribute("title").first().attr(
//				"title");
//		Element place = item.select("li.place").first();
//		place.select("a").remove();
//		String price = item.select("li.price em").first().text();
//		String postFee = item.select("li.price span").first().text();
//		postFee = HtmlParseUtil.getNumber(postFee);
//		String nick = item.select("span.J_WangWang").first().attr("data-nick");
//		String sale = item.select("li.sale").first().text();
//		// boolean mallIcon = !item.select("a.mall-icon").isEmpty();
//		String shopUrl = item.getElementsByAttributeValueContaining("href", "http://store.taobao.com/shop/view_shop.htm").first().attr("href");
//		String sellId = UrlUtil.getParam(shopUrl, "user_number_id");
//		sale = HtmlParseUtil.getNumber(sale);
//		TbItem itemTb = new TbItem();
//		itemTb.setPlace(place.text());
//		itemTb.setTbShopUrl(shopUrl);
//		itemTb.setDetailUrl(detailUrl);
//		itemTb.setSellerId(sellId);
//		itemTb.setTbItemId(tbItemId);
//		itemTb.setTitle(title);
//		itemTb.setListLogo(listLogo);
//		price = HtmlParseUtil.getNumber(price);
//		itemTb.setMinPrice(NumberUtil.toDouble(price));
//		itemTb.setMaxPrice(NumberUtil.toDouble(price));
//		itemTb.setPostFee(NumberUtil.toDouble(postFee));
//		itemTb.setSales(NumberUtil.toInt(sale));
//		itemTb.setSellerNick(nick);
//		//to do  shou parse
//		itemTb.setTbShopId(1);
//		return itemTb;
//	}

//	public void setTbItemService(TbItemService itemTbService) {
//		this.itemTbService = itemTbService;
//	}




	public void setTaobaoParseService(TaobaoParseService taobaoParseService) {
		this.taobaoParseService = taobaoParseService;
	}

	public void setTbItemService(TbItemService tbItemService) {
		this.tbItemService = tbItemService;
	}


	
	

	
	
	
}
